﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using Microsoft.Phone.Controls;

namespace FeatureOverview.Features.Sensors
{
    public partial class Accellerometer : PhoneApplicationPage
    {
        public Accellerometer()
        {
            InitializeComponent();

            this.Loaded += (s, e) =>
                {
                    StartSensor();
                };
        }

        Microsoft.Devices.Sensors.Accelerometer _acc;
            
        private void StartSensor()
        {
            if (Microsoft.Devices.Environment.DeviceType 
                == Microsoft.Devices.DeviceType.Emulator)
            {
                MessageBox.Show("Doesn't work in emulator");
                return;
            }

            _acc = new Microsoft.Devices.Sensors.Accelerometer();
            _acc.ReadingChanged += (s, e) =>
                {
                    Dispatcher.BeginInvoke(() =>
                        {
                            _x.Text = e.X.ToString();
                            _y.Text = e.Y.ToString();
                            _z.Text = e.Z.ToString();
                        });
                };
        }

        private void _on_Click(object sender, RoutedEventArgs e)
        {
            _acc.Start();
        }

        private void _off_Click(object sender, RoutedEventArgs e)
        {
            if (_acc != null)
            {
                _acc.Stop();
            }
        }
    }
}